package com.cl.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.cl.db.dao.NumAddressQueryTools;
import com.cl.safe.R;

public class NumAddressQueryActivity extends Activity {
	private EditText et_number;
	private TextView result;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_number_addres_query);
		et_number = (EditText) findViewById(R.id.et_phone);
		result = (TextView) findViewById(R.id.result);
		
		et_number.addTextChangedListener(new TextWatcher() {
			//当Edittext的文本发生了改变了
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if(s.length() >= 3) {
					String address = NumAddressQueryTools.queryNumber(s.toString());
					result.setText(address);
				}
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	/**
	 * 查询号码归属地
	 * @param view
	 */
	public void numberAddressQuery(View view) {
		String number = et_number.getText().toString().trim();
		if(TextUtils.isEmpty(number)) {
			//号码为空
			Toast.makeText(this, "号码为空了", 0).show();
		}else {
			//去数据库查询号码归属地
			//1.网络查询 ；2.本地的数据库--数据库
			//写一个工具类，去查询数据库
			String address = NumAddressQueryTools.queryNumber(number);
			result.setText(address);
		}
	}
}
